//
//  NewViewController.m
//  XiangMuB
//
//  Created by 999999999999 on 16/8/12.
//  Copyright © 2016年 lanou3g. All rights reserved.
//

#import "NewViewController.h"
#import "UIView+BSFrame.h"
#import "AllViewController.h"
#import "VideoViewController.h"
#import "HistoryViewController.h"
#import "PictureViewController.h"
#import "WordViewController.h"
#import "AmusementViewController.h"
#import "Content.h"

@interface NewViewController ()<UIScrollViewDelegate>
//标签栏底部的红色指示器
@property (nonatomic,weak)UIView *indicatorView;
//选中的当前按钮
@property (nonatomic,strong)UIButton *selectButton;
//顶部的所有标签
@property (nonatomic,strong)UIView *titlesView;
//底部的所有内容
@property (nonatomic,weak)UIScrollView *contentView;
@end

@implementation NewViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //view的背景色
    self.view.backgroundColor =[UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0];
    //导航栏
    [self setUpNav];
    
    //初始化子控制器
    [self setUpChildViews];
   
    //顶部标签栏
    [self setUpTitlesView];
    
    //底部的scrollView
    [self setUpContrntScrollView];
    
   
}
#pragma mark----初始化子控制器
-(void)setUpChildViews{
   
    VideoViewController *videoVC = [[VideoViewController alloc]init];
    videoVC.title = @"视频";
    videoVC.view.backgroundColor = [UIColor colorWithRed:223/255.0 green:223/255.0 blue:223/255.0 alpha:1.0];
    [self addChildViewController:videoVC];

    PictureViewController *picVC = [[PictureViewController alloc]init];
    picVC.title = @"图片";
    [self addChildViewController:picVC];
    
    AllViewController *allVC = [[AllViewController alloc]init];
    allVC.title =@"头条";
    [self addChildViewController:allVC];
    
    AmusementViewController *amusement = [[AmusementViewController alloc]init];
    amusement.title = @"娱乐";
    [self addChildViewController:amusement];
    
    WordViewController *wordVC = [[WordViewController alloc]init];
    wordVC.title = @"段子";
    [self addChildViewController:wordVC];
    
//    HistoryViewController *historyVC = [[HistoryViewController alloc]init];
//    historyVC.title = @"历史";
//    [self addChildViewController:historyVC];
   
    
}

#pragma mark ----设置顶部标签栏
-(void)setUpTitlesView{

    //标签栏整体
    UIView *titlesView = [[UIView alloc]init];
    //背景半透明
    titlesView.backgroundColor = [UIColor colorWithRed:236/255.0 green:236/255.0 blue:236/255.0 alpha:1.0];
    //或者
    //    titlesView.backgroundColor = [UIColor whiteColor:1.0 alpha:0.5];
    titlesView.width = self.view.width;
    titlesView.height = TitlesViewH;
    titlesView.y = TitlesViewY;
    [self.view addSubview:titlesView];
    self.titlesView = titlesView;

    //底部的红色的指示器
    UIView *indicatorView = [[UIView alloc]init];
    indicatorView.backgroundColor = [UIColor redColor];
    indicatorView.height = 2;
    indicatorView.y = titlesView.height  - indicatorView.height;
    
    //关联scroll
    indicatorView.tag = -1;
    self.indicatorView = indicatorView;

    
    //内部子标签
    CGFloat width = titlesView.width /self.childViewControllers.count;
    CGFloat height = titlesView.height;
    for (NSInteger i = 0 ; i < self.childViewControllers.count ; i ++) {
        UIButton *button = [[UIButton alloc]init];
//        button.backgroundColor = [UIColor orangeColor];
        button.height = height;
        button.width = width;
        button.x = i * width;
        //****索引
        button.tag = i;
        UIViewController *vc= self.childViewControllers[i];
        [button setTitle:vc.title forState:(UIControlStateNormal)];
        //*****文字设置完后强制布局
        [button layoutIfNeeded];//强制更新子空间的frame
        button.titleLabel.font = [UIFont systemFontOfSize:15];
        [button setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
        [button setTitleColor:[UIColor redColor] forState:(UIControlStateDisabled)]; // 选中后不能再选
        [button addTarget:self action:@selector(titleClick:) forControlEvents:(UIControlEventTouchUpInside)];
        [titlesView addSubview:button];
        //进入页面后默认点击第一个按钮
        if (i == 0) {
            button.enabled = NO;
            self.selectButton = button;
            //让按钮内部的lable根据文字的内容来计算尺寸
            [button.titleLabel sizeToFit]; //
            self.indicatorView.centerX = button.centerX;
            self.indicatorView.width = button.titleLabel.width;
//            self.indicatorView.width = [titles[i] sizeWithAttributes:@{NSHyphenationFactorDocumentAttribute:button.titleLabel.font}].width;
            self.indicatorView.x = button.titleLabel.x +3.0666;
//            NSLog(@"====%@",NSStringFromCGRect(self.indicatorView.frame));
        }
    }
    [titlesView addSubview:indicatorView];
}
-(void)titleClick:(UIButton *)button{
    //修改按钮的状态,,防止button重复点击
    self.selectButton.enabled = YES;
    button.enabled = NO;
    self.selectButton = button;
    //动画
    [UIView animateWithDuration:0.25 animations:^{
        self.indicatorView.centerX = button.centerX;
        self.indicatorView.width = button.titleLabel.width;
        //输出indicatorView的fram
//            NSLog(@"%@",NSStringFromCGRect(self.indicatorView.frame));
    }];
    //滚动,切换子控制器
    CGPoint offset = self.contentView.contentOffset;
    offset.x = button.tag * self.contentView.width;
    [self.contentView setContentOffset:offset animated:YES];
}
#pragma mark ---底部的scrollView
-(void)setUpContrntScrollView{
    
    //不需要自动调整inset
    self.automaticallyAdjustsScrollViewInsets = NO;
    UIScrollView *contentView = [[UIScrollView alloc]init];
    contentView.frame = self.view.bounds;
    //代理
    contentView.delegate = self;
   //分页
    contentView.pagingEnabled = YES;
    [self.view insertSubview:contentView atIndex:0];
////    //子控制器水平滚动
    contentView.contentSize = CGSizeMake(contentView.width * self.childViewControllers.count, 0);
    self.contentView = contentView;
   
#warning  运行时TableView显示的内容添加第一个控制器
    [self scrollViewDidEndScrollingAnimation:contentView];
    
}

#pragma mark ----自定义设置导航栏--
-(void)setUpNav{
    UIButton *btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [btn setBackgroundImage:[UIImage imageNamed:@"MainTagSubIcon"] forState:(UIControlStateNormal)];
    [btn setBackgroundImage:[UIImage imageNamed:@"MainTagSubIconClick"] forState:(UIControlStateHighlighted)];
    
    btn.size = btn.currentBackgroundImage.size;
    [btn addTarget:self action:@selector(newBtn) forControlEvents:(UIControlEventTouchUpInside)];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:btn];
    NSLog(@"%s",__func__);

}
//导航栏左边按钮
-(void)newBtn{
    NSLog(@"%s",__func__);
//    NSLog(@"333333");
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
#pragma mark ----UIScrollViewDelegate代理
-(void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    //添加子控制器的view

//    当前的索引
   NSInteger index = scrollView.contentOffset.x / scrollView.width;
    //取出子控制器
    UIViewController *vc = self.childViewControllers[index];
    vc.view.x = scrollView.contentOffset.x;
    vc.view.y = 0;
    vc.view.height = scrollView.height;
    [scrollView addSubview:vc.view];
}
//
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView {
    [self scrollViewDidEndScrollingAnimation:scrollView];
    //滑动tableview指示器跟随变化
    NSInteger index = scrollView.contentOffset.x / scrollView.width;
    [self titleClick:self.titlesView.subviews[index]];
}
@end
